package com.yb.bussiness.tribune;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.tribune.TribuneMaster;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class TribuneMasterServiceImpl implements TribuneMasterService {

	private IBaseDao<TribuneMaster, Long> dao;

	private String FIND_SQL ="SELECT * FROM T_G_TRIBUNE_MASTER WHERE 1=1 ";
	
	public TribuneMasterServiceImpl() {
		dao = new BaseDao<TribuneMaster, Long>(TribuneMaster.class);
	}
	
	public void delTriMaster(Long id) {
		dao.deleteById(id);
	}
	
	public TribuneMaster read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(TribuneMaster trif) {
			dao.saveOrUpdate(trif);
	}

	public List<TribuneMaster> findAll() {
		return dao.findList(FIND_SQL+" ORDER BY LASTVISITTIME");
	}

	public boolean deleteById(String id) {
		try {
			if ("".equals(id))
				return false;
			String sql = "delete from T_G_TRIBUNE_MASTER where id=? ";
			dao.delete(sql, new Object[] { id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from T_G_TRIBUNE_MASTER where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}
	
	public TribuneMaster getById(String id) {
		if ("".equals(id))
			return null;
		String sql = FIND_SQL+" and id=? ";
		try {
			List<TribuneMaster> list = dao.findList(sql, new Object[] { id });
			if (null != list && list.size() > 0) {
				TribuneMaster tp = list.get(0);
//				tp.setCreatetimeString(DateTimeUtil.switchDateToString(tp.getCreatetime(), "yyyy-MM-dd HH:mm:ss"));
//				tp.setOverTimeString(DateTimeUtil.switchDateToString(tp.getOverTime(), "yyyy-MM-dd HH:mm:ss"));
				return tp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public PageUtil getList(Map<String, String> map, int page, int limit){
		PageUtil pu = new PageUtil();
		if (map == null || map.size() == 0)
			return null;
		StringBuffer countSql = new StringBuffer();
		String FIND_SQL = "SELECT * FROM T_G_TRIBUNE_MASTER WHERE 1=1 ";
		countSql.append("select count(*) from t_g_tribune_master t where 1=1 ");
		if (null != map.get("userName") && !"".equals(map.get("userName"))) {
			FIND_SQL += "  and userName like '%" + map.get("userName") + "%'";
			countSql.append("  and userName like '%" + map.get("userName") + "%'");
		}
		if (null != map.get("auding") && !"".equals(map.get("auding"))) {
            FIND_SQL += "  and auding = " + map.get("auding") ;
            countSql.append("  and auding = " + map.get("auding"));
        }
		
		List<Object[]> countList = dao.findObjectArryList(countSql.toString());
		if (null != countList && countList.size() > 0) {
			Object[] countAry = countList.get(0);
			pu.setTotalRecords(new Integer(countAry[0].toString()));
		} else {
			pu.setTotalRecords(0);
		}
		List<TribuneMaster> newlist = null;
		if(pu.getTotalRecords()!=0){
			List<TribuneMaster> list = dao.findList(FIND_SQL
					+ " ORDER BY applyTime desc limit ?,?", new Object[] {
					(page - 1) * limit, limit });
			if (list != null && list.size() > 0) {
				newlist = new ArrayList<TribuneMaster>();
				for (TribuneMaster tm : list) {
					tm.setApplyTimeString(DateTimeUtil.switchDateToString(tm
							.getApplyTime(), "yyyy-MM-dd HH:mm:ss"));
					tm.setAudingTimeString(DateTimeUtil.switchDateToString(tm
							.getAudingTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tm);
				}
			}
		}else
			newlist = new ArrayList<TribuneMaster>();
		pu.setList(newlist);
		pu.setPageNo(page);
		pu.setPageSize(limit);
		return pu;
	}

	public boolean audingTribuneMaster(String auding, String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "update t_g_tribune_master set auding="+auding+" where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public TribuneMaster queryObjByUserId(Long id) {
			FIND_SQL+=" and userId=?";
			List<TribuneMaster> list = dao.findList(FIND_SQL, new Object[] { id });
			if(null!=list&&list.size()>0)
			return (TribuneMaster)list.get(0);
			return null;
	}

    public Integer loadpremind() {
        String sql = "select count(*) from t_g_tribune_master t where auding = 0";
        int total = Integer
        .parseInt((dao.findObjectArryList(sql).get(0))[0]
                .toString());
        return total;
    }

}
